<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label=" " prop="searchmonth">
        <el-input
          v-model="queryParams.searchmonth"
          placeholder="请输入要查询的时间(****-**)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" " prop="cinvcode">
        <el-input
          v-model="queryParams.cinvcode"
          placeholder="请输入要查询的存货编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['search:purchase:export']"
          style="margin-left: 70px;"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label=" " align="center" prop="ID" />-->

<!--      -&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-->
      <el-table-column label="存货分类" align="center" prop="partKind" />
      <el-table-column label="存货分类名称" align="center" prop="cInvCName" />
      <el-table-column label="存货编号" align="center" prop="cInvCode" />
      <el-table-column label="存货名称" align="center" prop="cInvName" />
      <el-table-column label="供应商名称" align="center" prop="cVenName" />
<!--      <el-table-column label="原币含税单价" align="center" prop="iOriTaxCost" />-->

      <el-table-column label="当月数量合计" align="center" prop="inqty" />
      <el-table-column label="历史采购入库原币含税单价" align="center" prop="ioritaxcost" />
      <el-table-column label="本币含税单价" align="center" prop="iOriTaxCost" />
      <el-table-column label="历史采购入库原币含税单价" align="center" prop="ioritaxcost1" />
      <el-table-column label="当前月入库数量*历史采购入库原币含税单价" align="center" prop="his" />
      <el-table-column label="当前月入库数量*当月采购入库原币含税单价" align="center" prop="ioriSum" />
      <el-table-column label="降本" align="center" prop="reduceCosts" />
<!--      <el-table-column label="历史采购入库原币含税单价" align="center" prop="ioritaxcost" />-->
<!--      <el-table-column label="当前月入库数量*历史采购入库原币含税单价" align="center" prop="ioritaxcost" />-->




<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['search:purchase:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['search:purchase:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase } from "@/api/search/purchase";

export default {
  name: "Purchase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购查询表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bRdFlag: null,
        cVouchType: null,
        cBusType: null,
        cSource: null,
        cBusCode: null,
        cWhCode: null,
        dDate: null,
        cCode: null,
        cRdCode: null,
        cDepCode: null,
        cPersonCode: null,
        cPTCode: null,
        cSTCode: null,
        cCusCode: null,
        cVenCode: null,
        cOrderCode: null,
        cARVCode: null,
        cBillCode: null,
        cDLCode: null,
        cProBatch: null,
        cHandler: null,
        cMemo: null,
        bTransFlag: null,
        cAccounter: null,
        cMaker: null,
        cDefine1: null,
        cDefine2: null,
        cDefine3: null,
        cDefine4: null,
        cDefine5: null,
        cDefine6: null,
        cDefine7: null,
        cDefine8: null,
        cDefine9: null,
        cDefine10: null,
        dKeepDate: null,
        dVeriDate: null,
        bpufirst: null,
        biafirst: null,
        iMQuantity: null,
        dARVDate: null,
        cChkCode: null,
        dChkDate: null,
        cChkPerson: null,
        searchmonth: null,
        vtId: null,
        bIsSTQc: null,
        cDefine11: null,
        cDefine12: null,
        cDefine13: null,
        cDefine14: null,
        cDefine15: null,
        cDefine16: null,
        gspcheck: null,
        ipurorderid: null,
        ipurarriveid: null,
        iarriveid: null,
        isalebillid: null,
        ufts: null,
        iTaxRate: null,
        iExchRate: null,
        cexchName: null,
        bOMFirst: null,
        bFromPreYear: null,
        bIsLsQuery: null,
        bIsComplement: null,
        iDiscountTaxType: null,
        ireturncount: null,
        iverifystate: null,
        iswfcontrolled: null,
        cModifyPerson: null,
        dModifyDate: null,
        dnmaketime: null,
        dnmodifytime: null,
        dnverifytime: null,
        bredvouch: null,
        cVenPUOMProtocol: null,
        dCreditStart: null,
        iCreditPeriod: null,
        dGatheringDate: null,
        bCredit: null,
        iFlowId: null,
        cPZID: null,
        cSourceLs: null,
        cSourceCodeLs: null,
        iPrintCount: null,
        csysbarcode: null,
        cCurrentAuditor: null,
        cCheckSignFlag: null,
        cGCRouteCode: null,
        csyssource: null,
        csyssourceid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ID: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
        bRdFlag: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
        cVouchType: [
          { required: true, message: " 不能为空", trigger: "change" }
        ],
        cSource: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
        cWhCode: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
        dDate: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
        cCode: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
        bTransFlag: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
        bIsSTQc: [
          { required: true, message: " 不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购查询列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: null,
        bRdFlag: null,
        cVouchType: null,
        cBusType: null,
        cSource: null,
        cBusCode: null,
        cWhCode: null,
        dDate: null,
        cCode: null,
        cRdCode: null,
        cDepCode: null,
        cPersonCode: null,
        cPTCode: null,
        cSTCode: null,
        cCusCode: null,
        cVenCode: null,
        cOrderCode: null,
        cARVCode: null,
        cBillCode: null,
        cDLCode: null,
        cProBatch: null,
        cHandler: null,
        cMemo: null,
        bTransFlag: null,
        cAccounter: null,
        cMaker: null,
        cDefine1: null,
        cDefine2: null,
        cDefine3: null,
        cDefine4: null,
        cDefine5: null,
        cDefine6: null,
        cDefine7: null,
        cDefine8: null,
        cDefine9: null,
        cDefine10: null,
        dKeepDate: null,
        dVeriDate: null,
        bpufirst: null,
        biafirst: null,
        iMQuantity: null,
        dARVDate: null,
        cChkCode: null,
        dChkDate: null,
        cChkPerson: null,
        vtId: null,
        bIsSTQc: null,
        cDefine11: null,
        cDefine12: null,
        cDefine13: null,
        cDefine14: null,
        cDefine15: null,
        cDefine16: null,
        gspcheck: null,
        ipurorderid: null,
        ipurarriveid: null,
        iarriveid: null,
        isalebillid: null,
        ufts: null,
        iTaxRate: null,
        iExchRate: null,
        cexchName: null,
        bOMFirst: null,
        bFromPreYear: null,
        bIsLsQuery: null,
        bIsComplement: null,
        iDiscountTaxType: null,
        ireturncount: null,
        iverifystate: null,
        iswfcontrolled: null,
        cModifyPerson: null,
        dModifyDate: null,
        dnmaketime: null,
        dnmodifytime: null,
        dnverifytime: null,
        bredvouch: null,
        cVenPUOMProtocol: null,
        dCreditStart: null,
        iCreditPeriod: null,
        dGatheringDate: null,
        bCredit: null,
        iFlowId: null,
        cPZID: null,
        cSourceLs: null,
        cSourceCodeLs: null,
        iPrintCount: null,
        csysbarcode: null,
        cCurrentAuditor: null,
        cCheckSignFlag: null,
        cGCRouteCode: null,
        csyssource: null,
        csyssourceid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ID)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ID = row.ID || this.ids
      getPurchase(ID).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购查询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ID != null) {
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.ID || this.ids;
      this.$modal.confirm('是否确认删除采购查询编号为"' + IDs + '"的数据项？').then(function() {
        return delPurchase(IDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('search/purchase/export', {
        ...this.queryParams
      }, `采购入库降本_${new Date().getTime().toString()}.xlsx`)
    }
  }
};
</script>
